#if 0
#include <ros/ros.h>
#include <chrono>
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <cmath>

// Protobuf 相关头文件
#include <ros/protobuffer_traits.h>
#include <ros/serialization_protobuffer.h>
#include "publish_info.pb.h" // 你的 Protobuf 消息头文件

// ROS 原生消息相关头文件
#include <myproject/Person.h> // 你的 ROS 原生消息头文件

// 用于存储时间的全局向量
std::vector<long long> proto_deserialization_times_ns;
std::vector<long long> proto_e2e_latencies_ns; // 存储 Protobuf 端到端延迟

std::vector<long long> ros_native_deserialization_times_ns;
std::vector<long long> ros_native_e2e_latencies_ns; // 存储 ROS 原生端到端延迟

#define DESERIALIZATION_COUNT_THRESHOLD 100 // 收集样本数量阈值

// Protobuf 消息回调函数
void protoMessageCallback(const ros::MessageEvent<pbtest::efftest>& received_msg) {
    // --- 开始计时反序列化 (同之前) ---
    auto start_deserialize = std::chrono::high_resolution_clock::now();
    std::string debug_str = received_msg.getMessage()->DebugString(); // 强制访问
    auto end_deserialize = std::chrono::high_resolution_clock::now();
    proto_deserialization_times_ns.push_back(std::chrono::duration_cast<std::chrono::nanoseconds>(end_deserialize - start_deserialize).count());

    // --- 计算端到端延迟 ---
    ros::Time receive_time = ros::Time::now(); // 收到消息并进入回调的当前时间
    ros::Time send_time;
    send_time.fromNSec(received_msg.getMessage()->send_timestamp_ns()); // 从 Protobuf 消息中获取发送时间戳

    ros::Duration latency = receive_time - send_time;
    proto_e2e_latencies_ns.push_back(latency.toNSec()); // 记录端到端延迟

    // 检查是否达到阈值，并关闭 ROS (如果两个计数都达到)
    if (DESERIALIZATION_COUNT_THRESHOLD > 0 && 
        proto_e2e_latencies_ns.size() >= DESERIALIZATION_COUNT_THRESHOLD &&
        ros_native_e2e_latencies_ns.size() >= DESERIALIZATION_COUNT_THRESHOLD) // 等待两个测试都完成
    {
        ROS_INFO("Collected all deserialization and E2E latency samples. Shutting down.");
        ros::shutdown();
    }
}

// ROS 原生消息回调函数
void rosNativeMessageCallback(const myproject::Person::ConstPtr& received_msg) {
    // --- 开始计时反序列化 (同之前) ---
    auto start_deserialize = std::chrono::high_resolution_clock::now();
    std::string name_accessed = received_msg->name; // 强制访问
    auto end_deserialize = std::chrono::high_resolution_clock::now();
    ros_native_deserialization_times_ns.push_back(std::chrono::duration_cast<std::chrono::nanoseconds>(end_deserialize - start_deserialize).count());

    // --- 计算端到端延迟 ---
    ros::Time receive_time = ros::Time::now(); // 收到消息并进入回调的当前时间
    ros::Time send_time = received_msg->header.stamp; // 从 ROS 消息头获取发送时间戳

    ros::Duration latency = receive_time - send_time;
    ros_native_e2e_latencies_ns.push_back(latency.toNSec()); // 记录端到端延迟

    // 检查是否达到阈值，并关闭 ROS (如果两个计数都达到)
    if (DESERIALIZATION_COUNT_THRESHOLD > 0 && 
        proto_e2e_latencies_ns.size() >= DESERIALIZATION_COUNT_THRESHOLD &&
        ros_native_e2e_latencies_ns.size() >= DESERIALIZATION_COUNT_THRESHOLD) // 等待两个测试都完成
    {
        ROS_INFO("Collected all deserialization and E2E latency samples. Shutting down.");
        ros::shutdown();
    }
}

int main(int argc, char** argv) {
    ros::init(argc, argv, "end_to_end_subscriber_node");
    ros::NodeHandle nh;

    // 订阅 Protobuf 话题
    ros::Subscriber pb_sub = nh.subscribe("/my_protobuf_topic", 1000, protoMessageCallback);
    // 订阅 ROS 原生话题
    ros::Subscriber ros_native_sub = nh.subscribe("/my_ros_native_topic", 1000, rosNativeMessageCallback);

    ROS_INFO("订阅节点已启动。等待消息...");
    ROS_INFO("将收集 %d 个样本然后打印平均延迟。", DESERIALIZATION_COUNT_THRESHOLD);

    ros::spin(); // 保持节点运行

    // --- 统计并打印 Protobuf 结果 ---
    if (!proto_e2e_latencies_ns.empty()) {
        long long total_e2e_ns = 0; for(long long t : proto_e2e_latencies_ns) total_e2e_ns += t;
        double avg_e2e_ns = static_cast<double>(total_e2e_ns) / proto_e2e_latencies_ns.size();
        
        long long total_des_ns = 0; for(long long t : proto_deserialization_times_ns) total_des_ns += t;
        double avg_des_ns = static_cast<double>(total_des_ns) / proto_deserialization_times_ns.size();

        std::cout << "\n--- Protobuf 性能测试结果 ---" << std::endl;
        std::cout << "总接收消息数: " << proto_e2e_latencies_ns.size() << std::endl;
        std::cout << "平均反序列化耗时: " << avg_des_ns / 1000.0 << " 微秒" << std::endl;
        std::cout << "平均端到端延迟: " << avg_e2e_ns / 1000.0 << " 微秒" << std::endl;
        if (proto_e2e_latencies_ns.size() > 1) {
            double sum_sq_diff = 0.0;
            for (long long time_ns : proto_e2e_latencies_ns) sum_sq_diff += std::pow(time_ns - avg_e2e_ns, 2);
            double std_dev_ns = std::sqrt(sum_sq_diff / proto_e2e_latencies_ns.size());
            std::cout << "端到端延迟标准差: " << std_dev_ns / 1000.0 << " 微秒" << std::endl;
        }
    } else {
        std::cout << "没有接收到 Protobuf 消息进行测试。" << std::endl;
    }

    // --- 统计并打印 ROS 原生结果 ---
    if (!ros_native_e2e_latencies_ns.empty()) {
        long long total_e2e_ns = 0; for(long long t : ros_native_e2e_latencies_ns) total_e2e_ns += t;
        double avg_e2e_ns = static_cast<double>(total_e2e_ns) / ros_native_e2e_latencies_ns.size();
        
        long long total_des_ns = 0; for(long long t : ros_native_deserialization_times_ns) total_des_ns += t;
        double avg_des_ns = static_cast<double>(total_des_ns) / ros_native_deserialization_times_ns.size();

        std::cout << "\n--- ROS 原生消息性能测试结果 ---" << std::endl;
        std::cout << "总接收消息数: " << ros_native_e2e_latencies_ns.size() << std::endl;
        std::cout << "平均反序列化耗时: " << avg_des_ns / 1000.0 << " 微秒" << std::endl;
        std::cout << "平均端到端延迟: " << avg_e2e_ns / 1000.0 << " 微秒" << std::endl;
        if (ros_native_e2e_latencies_ns.size() > 1) {
            double sum_sq_diff = 0.0;
            for (long long time_ns : ros_native_e2e_latencies_ns) sum_sq_diff += std::pow(time_ns - avg_e2e_ns, 2);
            double std_dev_ns = std::sqrt(sum_sq_diff / ros_native_e2e_latencies_ns.size());
            std::cout << "端到端延迟标准差: " << std_dev_ns / 1000.0 << " 微秒" << std::endl;
        }
    } else {
        std::cout << "没有接收到 ROS 原生消息进行测试。" << std::endl;
    }

    return 0;
}
#endif


#if 0
#include <ros/ros.h>
#include <chrono>
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <cmath>

// Protobuf 相关头文件
#include <ros/protobuffer_traits.h>
#include <ros/serialization_protobuffer.h>
#include "publish_info.pb.h" // 你的 Protobuf 消息头文件 (确保已添加 send_timestamp_ns 字段)

// ROS 原生消息相关头文件
#include <myproject/Person.h> // 你的 ROS 原生消息头文件 (确保已添加 header 字段)

// 用于存储时间的全局向量
std::vector<long long> proto_deserialization_times_ns;
std::vector<long long> proto_e2e_latencies_ns; // 存储 Protobuf 端到端延迟
std::vector<long long> proto_received_serialized_sizes; // 【新增】存储 Protobuf 接收到的序列化大小

std::vector<long long> ros_native_deserialization_times_ns;
std::vector<long long> ros_native_e2e_latencies_ns; // 存储 ROS 原生端到端延迟
std::vector<long long> ros_native_received_serialized_sizes; // 【新增】存储 ROS 原生接收到的序列化大小


#define DESERIALIZATION_COUNT_THRESHOLD 100 // 收集样本数量阈值

// Protobuf 消息回调函数
void protoMessageCallback(const ros::MessageEvent<pbtest::efftest>& received_msg_event) { // 修改参数类型为 pbtest::PublishInfo
    // 【新增】获取接收到的序列化消息大小
    // ros::MessageEvent 允许访问底层传输信息，包括原始消息数据的长度
    size_t received_serialized_size = received_msg_event.getMessage()->ByteSizeLong();
    proto_received_serialized_sizes.push_back(received_serialized_size);

    // 获取实际消息对象
    const auto& received_proto_msg = received_msg_event.getMessage();

    // --- 开始计时反序列化 (同之前) ---
    auto start_deserialize = std::chrono::high_resolution_clock::now();
    std::string debug_str = received_proto_msg->DebugString(); // 强制访问
    auto end_deserialize = std::chrono::high_resolution_clock::now();
    proto_deserialization_times_ns.push_back(std::chrono::duration_cast<std::chrono::nanoseconds>(end_deserialize - start_deserialize).count());

    // --- 计算端到端延迟 ---
    ros::Time receive_time = ros::Time::now(); // 收到消息并进入回调的当前时间
    ros::Time send_time;
    // 确保你的 .proto 文件中 `send_timestamp_ns` 字段正确，并且已重新生成 Protobuf C++ 文件
    send_time.fromNSec(received_proto_msg->send_timestamp_ns()); // 从 Protobuf 消息中获取发送时间戳

    ros::Duration latency = receive_time - send_time;
    proto_e2e_latencies_ns.push_back(latency.toNSec()); // 记录端到端延迟

    // 检查是否达到阈值，并关闭 ROS (如果两个计数都达到)
    if (DESERIALIZATION_COUNT_THRESHOLD > 0 && 
        proto_e2e_latencies_ns.size() >= DESERIALIZATION_COUNT_THRESHOLD &&
        ros_native_e2e_latencies_ns.size() >= DESERIALIZATION_COUNT_THRESHOLD) // 等待两个测试都完成
    {
        ROS_INFO("Collected all deserialization and E2E latency samples. Shutting down.");
        ros::shutdown();
    }
}

// ROS 原生消息回调函数
void rosNativeMessageCallback(const ros::MessageEvent<myproject::Person>& received_msg_event) { // 修改参数类型为 myproject::Person
    // 【新增】获取接收到的序列化消息大小
    auto msg = received_msg_event.getMessage();
    std::size_t serialized_size = ros::serialization::serializationLength(*msg);
    ros_native_received_serialized_sizes.push_back(serialized_size);

    // 获取实际消息对象
    const auto& received_ros_msg = received_msg_event.getMessage();

    // --- 开始计时反序列化 (同之前) ---
    auto start_deserialize = std::chrono::high_resolution_clock::now();
    std::string name_accessed = received_ros_msg->name; // 强制访问
    auto end_deserialize = std::chrono::high_resolution_clock::now();
    ros_native_deserialization_times_ns.push_back(std::chrono::duration_cast<std::chrono::nanoseconds>(end_deserialize - start_deserialize).count());

    // --- 计算端到端延迟 ---
    ros::Time receive_time = ros::Time::now(); // 收到消息并进入回调的当前时间
    // 确保你的 Person.msg 中 `header` 字段正确，并且已重新编译 ROS 包
    ros::Time send_time = received_ros_msg->header.stamp; // 从 ROS 消息头获取发送时间戳

    ros::Duration latency = receive_time - send_time;
    ros_native_e2e_latencies_ns.push_back(latency.toNSec()); // 记录端到端延迟

    // 检查是否达到阈值，并关闭 ROS (如果两个计数都达到)
    if (DESERIALIZATION_COUNT_THRESHOLD > 0 && 
        proto_e2e_latencies_ns.size() >= DESERIALIZATION_COUNT_THRESHOLD &&
        ros_native_e2e_latencies_ns.size() >= DESERIALIZATION_COUNT_THRESHOLD) // 等待两个测试都完成
    {
        ROS_INFO("Collected all deserialization and E2E latency samples. Shutting down.");
        ros::shutdown();
    }
}

int main(int argc, char** argv) {
    ros::init(argc, argv, "end_to_end_subscriber_node");
    ros::NodeHandle nh;

    // 订阅 Protobuf 话题
    // 注意: 回调函数参数类型从 'const ros::MessageEvent<pbtest::efftest>&' 改为 'const ros::MessageEvent<pbtest::PublishInfo>&'
    // 这是根据你之前proto文件的修改，PublishInfo才是你的消息类型
    ros::Subscriber pb_sub = nh.subscribe("/my_protobuf_topic", 1000, protoMessageCallback);
    
    // 订阅 ROS 原生话题
    // 注意: 回调函数参数类型从 'const myproject::Person::ConstPtr&' 改为 'const ros::MessageEvent<myproject::Person>&'
    ros::Subscriber ros_native_sub = nh.subscribe("/my_ros_native_topic", 1000, rosNativeMessageCallback);

    ROS_INFO("订阅节点已启动。等待消息...");
    ROS_INFO("将收集 %d 个样本然后打印平均延迟。", DESERIALIZATION_COUNT_THRESHOLD);

    ros::spin(); // 保持节点运行

    // --- 统计并打印 Protobuf 结果 ---
    if (!proto_e2e_latencies_ns.empty()) {
        long long total_e2e_ns = 0; for(long long t : proto_e2e_latencies_ns) total_e2e_ns += t;
        double avg_e2e_ns = static_cast<double>(total_e2e_ns) / proto_e2e_latencies_ns.size();
        
        long long total_des_ns = 0; for(long long t : proto_deserialization_times_ns) total_des_ns += t;
        double avg_des_ns = static_cast<double>(total_des_ns) / proto_deserialization_times_ns.size();

        long long total_received_pb_size = 0; for(long long s : proto_received_serialized_sizes) total_received_pb_size += s;
        double avg_received_pb_size = static_cast<double>(total_received_pb_size) / proto_received_serialized_sizes.size();


        std::cout << "\n--- Protobuf 性能测试结果 ---" << std::endl;
        std::cout << "总接收消息数: " << proto_e2e_latencies_ns.size() << std::endl;
        std::cout << "**平均接收消息序列化大小: " << avg_received_pb_size << " 字节**" << std::endl; // 【新增】
        std::cout << "平均反序列化耗时: " << avg_des_ns / 1000.0 << " 微秒" << std::endl;
        std::cout << "平均端到端延迟: " << avg_e2e_ns / 1000.0 << " 微秒" << std::endl;
        if (proto_e2e_latencies_ns.size() > 1) {
            double sum_sq_diff = 0.0;
            for (long long time_ns : proto_e2e_latencies_ns) sum_sq_diff += std::pow(time_ns - avg_e2e_ns, 2);
            double std_dev_ns = std::sqrt(sum_sq_diff / proto_e2e_latencies_ns.size());
            std::cout << "端到端延迟标准差: " << std_dev_ns / 1000.0 << " 微秒" << std::endl;
        }
    } else {
        std::cout << "没有接收到 Protobuf 消息进行测试。" << std::endl;
    }

    // --- 统计并打印 ROS 原生结果 ---
    if (!ros_native_e2e_latencies_ns.empty()) {
        long long total_e2e_ns = 0; for(long long t : ros_native_e2e_latencies_ns) total_e2e_ns += t;
        double avg_e2e_ns = static_cast<double>(total_e2e_ns) / ros_native_e2e_latencies_ns.size();
        
        long long total_des_ns = 0; for(long long t : ros_native_deserialization_times_ns) total_des_ns += t;
        double avg_des_ns = static_cast<double>(total_des_ns) / ros_native_deserialization_times_ns.size();

        long long total_received_ros_size = 0; for(long long s : ros_native_received_serialized_sizes) total_received_ros_size += s;
        double avg_received_ros_size = static_cast<double>(total_received_ros_size) / ros_native_received_serialized_sizes.size();

        std::cout << "\n--- ROS 原生消息性能测试结果 ---" << std::endl;
        std::cout << "总接收消息数: " << ros_native_e2e_latencies_ns.size() << std::endl;
        std::cout << "**平均接收消息序列化大小: " << avg_received_ros_size << " 字节**" << std::endl; // 【新增】
        std::cout << "平均反序列化耗时: " << avg_des_ns / 1000.0 << " 微秒" << std::endl;
        std::cout << "平均端到端延迟: " << avg_e2e_ns / 1000.0 << " 微秒" << std::endl;
        if (ros_native_e2e_latencies_ns.size() > 1) {
            double sum_sq_diff = 0.0;
            for (long long time_ns : ros_native_e2e_latencies_ns) sum_sq_diff += std::pow(time_ns - avg_e2e_ns, 2);
            double std_dev_ns = std::sqrt(sum_sq_diff / ros_native_e2e_latencies_ns.size());
            std::cout << "端到端延迟标准差: " << std_dev_ns / 1000.0 << " 微秒" << std::endl;
        }
    } else {
        std::cout << "没有接收到 ROS 原生消息进行测试。" << std::endl;
    }

    return 0;
}
#endif
